﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using IAM.Interface.Model;
using System.Reflection;
using System.Collections;
using IAM.Interface.Model.ENUM;
using IAM.Interface.Model.Equals;
using System.Web;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 所有用户账号权限信息列表
    /// </summary>
    public class UserACIs : AbstractConvertObject, IConvertXml
    {
        #region 构造函数
        public UserACIs()
        {
            _useracis = new System.Collections.ArrayList();
        }
        #endregion
        #region 属性
        private System.Collections.ArrayList _useracis = null;
        private System.Collections.ArrayList _compareresultlist;
        /// <summary>
        /// UserACI的数量
        /// </summary>
        public int Count
        {
            get
            {
                return this.Items.Count;
            }
        }
        /// <summary>
        /// 单个用户的账号权限信息【UserACI集合】
        /// 包含2部分内容：AccountInfo和ACSessions
        /// </summary>
        public System.Collections.ArrayList Items
        {
            get
            {
                return _useracis;
            }
            set
            {
                _useracis = value;
            }
        }
        /// <summary>
        /// 比较结果集,包含CompareResult对象
        /// </summary>
        public System.Collections.ArrayList CompareresultList
        {
            get
            {
                return _compareresultlist;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <UserACIs ");
                _xml.Append(" Count=\"" + this.Count + "\"");
                _xml.Append(" >");
                foreach (object obj in this.Items)
                {
                    _xml.Append(((UserACI)obj).XML);
                }
                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</UserACIs>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                UserACI useraci = new UserACI();
                useraci.CurrentXmlNode = xn;
                useraci = (UserACI)useraci.ConvertToObject();
                this.Items.Add(useraci);
            }
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 UserACIs 对象是否等效于当前 UserACIs
        /// 1.验证类型是否一致
        /// 2.验证集合数量是否一致
        /// 3.验证集合中对象是否一致【无顺比较】
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            bool boolforeach = true;
            //1.验证类型是否一致
            UserACIs ua = null;
            try
            {
                ua = (UserACIs)obj;
            }
            catch
            {
                boolforeach = false;
            }
            //2.验证集合数量是否一致
            if (this.Items.Count != ua.Items.Count)
            {
                boolforeach = false;
            }


            //3.验证集合中对象是否一致【无顺比较】
            if (boolforeach)
            {
                foreach (object rudeobj in this.Items)
                {
                    boolforeach = false;
                    foreach (object aimobj in ua.Items)
                    {
                        if (((UserACI)rudeobj).Equals(aimobj))
                        {
                            boolforeach = true;
                            break;
                        }
                    }
                    if (boolforeach == false)
                    {
                        break;
                    }
                }
            }
            return boolforeach;

        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// 应为可能存在集合的顺序问题此函数不能作为对象的唯一标志
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
        #region 比较并记录对象的不同处
        /// <summary>
        /// 比较并记录对象的不同处
        /// 1.将source xml化
        /// 2.将 obj xml化
        /// 3. 实例化CompareresultList
        /// 4. 遍历source的AccountInfo
        /// 4.1 在obj中查找对应的AccountInfo
        /// 4.2 if (没有找到)
        /// 4.2.1 将不同记录到 CompareresultList中
        /// 4.2.2 else( 比较相同用户的权限信息是否相同)
        /// 4.2.3 如果权限不同，将不同记录到 CompareresultList中
        /// end4.2
        /// 5.遍历 obj AccountInfo
        /// 5.1 类似4.1操作
        /// 6 如果有不想同的项目查看CompareresultList是否存在，如果不存在 将之添加
        /// </summary>
        /// <param name="source">源对象</param>
        /// <param name="obj">目标对象</param>
        /// <param name="indextype">人员信息主键</param>
        public ArrayList GetCompareResultList(UserACIs source, UserACIs obj, IndexType indextype)
        {
            CompareResult cr = null;
            //1.将source xml化
            XmlDocument xdsource = new XmlDocument();
            xdsource.LoadXml(source.XML);

            //2.将 obj xml化
            XmlDocument xdobj = new XmlDocument();
            xdobj.LoadXml(obj.XML);
            //3. 实例化CompareresultList
            _compareresultlist = new ArrayList();
            #region 4. 遍历source的AccountInfo
            //4. 遍历source的AccountInfo
            foreach (object _source in source.Items)
            {
                UserACI useracisource = (UserACI)_source;
                string key = "";
                switch (indextype)
                {
                    case IndexType.ADAccount:
                        key = useracisource.AccountInfo.ADAccount;
                        break;
                    case IndexType.CILX500:
                        key = useracisource.AccountInfo.CILX500;
                        break;
                    case IndexType.CSLAccount:
                        key = useracisource.AccountInfo.CSLAccount;
                        break;
                    case IndexType.HRISEID:
                        key = useracisource.AccountInfo.HRISEID;
                        break;
                    case IndexType.UPI:
                        key = useracisource.AccountInfo.UPI;
                        break;

                }
                key = IAM.Interface.Common.EnDeCoding.Encode(key);
                //4.1 在obj中查找对应的AccountInfo
                XmlNode xmlnode = xdobj.SelectSingleNode("//AccountInfo[" + indextype.ToString() + "='" + @key + "']");
                //4.2 if (没有找到)
                if (xmlnode == null)
                {
                    //4.2.1 将不同记录到 CompareresultList中
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = false;
                    cr.ContenctEquation = false;
                    _compareresultlist.Add(cr);
                }//end4.2
                //4.2.2 else( 比较相同用户的权限信息是否相同)
                else
                {
                    UserACI useraciobj = new UserACI();
                    useraciobj.CurrentXmlNode = xmlnode.ParentNode;
                    useraciobj = (UserACI)useraciobj.ConvertToObject();

                    //4.2.3 如果权限不同，将不同记录到 CompareresultList中
                    if(!useracisource.Equals(useraciobj))
                    {
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;
                        _compareresultlist.Add(cr);
                    }
                }
            }
            #endregion
            #region 遍历 obj AccountInfo
            //5.遍历 obj AccountInfo
            foreach (object objofitems in obj.Items)
            {
                UserACI useraciobj = (UserACI)objofitems;
                string key = "";
                switch (indextype)
                {
                    case IndexType.ADAccount:
                        key = useraciobj.AccountInfo.ADAccount;
                        break;
                    case IndexType.CILX500:
                        key = useraciobj.AccountInfo.CILX500;
                        break;
                    case IndexType.CSLAccount:
                        key = useraciobj.AccountInfo.CSLAccount;
                        break;
                    case IndexType.HRISEID:
                        key = useraciobj.AccountInfo.HRISEID;
                        break;
                    case IndexType.UPI:
                        key = useraciobj.AccountInfo.UPI;
                        break;

                }
                key = IAM.Interface.Common.EnDeCoding.Encode(key);
                //5.1 在source中查找对应的AccountInfo
                XmlNode xmlnode = xdsource.SelectSingleNode("//AccountInfo[" + indextype.ToString() + "='" + @key + "']");
                //5.2 if (没有找到)
                if (xmlnode == null)
                {
                    //5.2.1 将不同记录到 CompareresultList中
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = false;
                    cr.InObject = true;
                    cr.ContenctEquation = false;
                    _compareresultlist.Add(cr);
                }//end5.2
                //5.2.2 else( 比较相同用户的权限信息是否相同)
                else
                {
                    UserACI useracisource = new UserACI();
                    useracisource.CurrentXmlNode = xmlnode.ParentNode;
                    useracisource = (UserACI)useracisource.ConvertToObject();

                    //5.2.3 如果权限不同，将不同记录到 CompareresultList中
                    if (!useraciobj.Equals(useracisource))
                    {
                        foreach (object _compareesult in _compareresultlist)
                        {
                            if (((CompareResult)_compareesult).ID == key)
                            {
                                _compareresultlist.Remove(_compareesult);
                                break;
                            }
                        }
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;
                        _compareresultlist.Add(cr);
                    }
                }
            }
            #endregion
            //6 如果有不想同的项目查看CompareresultList是否存在，如果不存在 将之添加
            if (_compareresultlist.Count > 0)
            {
                return _compareresultlist;
            }
            else
            {
                _compareresultlist = null;
                return _compareresultlist;
            }
        }
        #endregion
    }
}
